<?php
defined ( 'IN_KEKE' ) or exit ( 'Access Denied' );
keke_core_class::check_login();
require_once(S_ROOT.'/hgb_inform/jsapi.php');
global $htxsession;
$umoneyupoints = hgb_jsgetumoneyupoints($htxsession['uc_uid']);
$type = strval ( trim ( $type ) );
$id = intval ( trim ( $id ) );
switch ($type) {
	case 'hosted':
		$arrTaskInfo = db_factory::get_one ( sprintf ( "select * from %switkey_task where task_id='%d'", TABLEPRE, $id ) );
		$modelInfo = $kekezu->_model_list [$arrTaskInfo ['model_id']];
		$className = $modelInfo ['model_code'] . "_task_class";
		$arrOrderDetailInfo = db_factory::get_one ( sprintf ( "select order_id from %switkey_order_detail where obj_id=%d and obj_type = 'hosted'", TABLEPRE, $id ) );
		$orderId = intval ( $arrOrderDetailInfo ['order_id'] );
		$arrOrderInfo = db_factory::get_one ( sprintf ( "select * from %switkey_order where order_id=%d ", TABLEPRE, $orderId ) );
		$cash = $arrOrderInfo ['order_amount'];
		$title = $arrTaskInfo ['task_title'];
		$objId = $arrTaskInfo ['task_id'];
		$modelId = $arrTaskInfo ['model_id'];
		break;
	case 'task' :
		$arrTaskInfo = db_factory::get_one ( sprintf ( "select * from %switkey_task where task_id='%d'", TABLEPRE, $id ) );
		$modelInfo = $kekezu->_model_list [$arrTaskInfo ['model_id']];
		$className = $modelInfo ['model_code'] . "_task_class";
		$arrOrderDetailInfo = db_factory::get_one ( sprintf ( "select order_id from %switkey_order_detail where obj_id=%d and obj_type = 'task'", TABLEPRE, $id ) );
		$orderId = intval ( $arrOrderDetailInfo ['order_id'] );
		$arrOrderInfo = db_factory::get_one ( sprintf ( "select * from %switkey_order where order_id=%d ", TABLEPRE, $orderId ) );
		$cash = $arrOrderInfo ['order_amount'];
		$title = $arrTaskInfo ['task_title'];
		$objId = $arrTaskInfo ['task_id'];
		$modelId = $arrTaskInfo ['model_id'];
		break;
	case 'order' :
		$arrOrderInfo = db_factory::get_one ( sprintf ( "select order_id,order_amount,order_name,order_uid from %switkey_order where order_id=%d ", TABLEPRE, $id ) );
		$arrOrderDetailInfo = db_factory::get_one ( sprintf ( "select obj_id from %switkey_order_detail where order_id=%d ", TABLEPRE, $id ) );
		$arrServiceInfo = db_factory::get_one ( sprintf ( "select model_id from %switkey_service where service_id=%d ", TABLEPRE, $arrOrderDetailInfo ['obj_id'] ) );
		$orderId = intval ( $arrOrderInfo ['order_id'] );
		$cash = floatval ( $arrOrderInfo ['order_amount'] );
		$title = strval ( $arrOrderInfo ['order_name'] );
		$objId = intval ( $arrOrderDetailInfo ['obj_id'] );
		$modelId = intval ( $arrServiceInfo ['model_id'] );
		break;
	case 'payitem' :
		$arrOrderInfo = db_factory::get_one ( sprintf ( "select order_id,order_amount,order_name,order_uid from %switkey_order where order_id=%d ", TABLEPRE, $id ) );
		$arrOrderDetailInfo = db_factory::get_one ( sprintf ( "select obj_id from %switkey_order_detail where order_id=%d ", TABLEPRE, $id ) );
		$orderId = intval ( $arrOrderInfo ['order_id'] );
		$cash = floatval ( $arrOrderInfo ['order_amount'] );
		$title = strval ( $arrOrderInfo ['order_name'] );
		$objId = intval ( $arrOrderDetailInfo ['obj_id'] );
		$modelId = intval ( $arrServiceInfo ['model_id'] );
}
if($gUid != $arrOrderInfo['order_uid']){
	kekezu::show_msg('页面不存在','',3,null,'warning');
}
if ($_R['status'] == '2') {
    $status = 2;
} else {
    $status = 1;
}
$strUrl = 'index.php?do=pay&type=' . $type . '&id=' . $id;
if (isset($formhash ) && kekezu::submitcheck ( $formhash )) {
    if ($umoneyupoints && $umoneyupoints['umoney'] >= $cash) {
        if (hgb_jscheckpwd($htxsession['uc_uid'], $_R['pwd'])) {
            if ($type == 'task') {
                $payTitle = '-发布任务-'.substr($title,0,64);
            } elseif ($type == 'payitem') {
                $payTitle = '-增值工具-'.substr($title,0,64);
            }else if($type=='order'){
                $payTitle = '-购买服务-'.substr($title,0,64);
            }else if($type=='hosted'){
                $payTitle = '-托管金额-'.substr($title,0,64);
            }        
            $model_id = intval($model_id);
            $charge_type = 'order_charge';
            if($type == 'payitem'){
                $charge_type = 'payitem_charge';
            }
            $fac_obj = new pay_return_fac_class($charge_type, $modelId, $arrOrderInfo['order_uid'], $objId, $orderId, $cash,'hgb');
            $response = $fac_obj->load();
            if($charge_type=='user_charge'){
                $show_url = 'index.php?do=recharge&cash='.$total_fee.'&status=1';
            }elseif($charge_type=='payitem_charge'){
                if(! in_array($model_id, array(6,7))){
                    $show_url = 'index.php?do=task&id='.$id;
                }else{
                    $show_url =  'index.php?do=goods&id='.$id;
                }
            }else{
                if(!in_array($model_id, array(6,7))){
                    $arrOrderDetail = keke_order_class::get_order_detail($order_id);
                    if($arrOrderDetail[0]['obj_type']=='hosted'){
                        $show_url = 'index.php?do=task&id='.$id;
                    }else{
                        $show_url = 'index.php?do=pay&type=task&id='.$id.'&status=2';
                    }
                }else{
                    $show_url =  'index.php?do=pay&type=order&id='.$orderid.'&status=2';
                }
            }
            $response['url'] =$_K['siteurl'].'/'.$show_url;
            header('Location:'.$response['url']);
            exit();
        } else {
            echo '<script>alert("密码错误");</script>';
        }
    } else {
        echo '<script>alert("可用余额不足以支付,请充值！");</script>';
    }
	/*$bankConfig = kekezu::get_payment_config ( $bank );
	require S_ROOT . "/include/payment/" . $bank . "/order.php";
	if($type == 'payitem'){
		$form = get_pay_url ( 'payitem_charge', $cash, $bankConfig, $payTitle, $orderId, $modelId, $objId, NULL, 'MD5', 'form' );
	}else{
		$form = get_pay_url ( 'order_charge', $cash, $bankConfig, $payTitle, $orderId, $modelId, $objId, NULL, 'MD5', 'form' );
	}
	echo $form;
	die ();*/
        
}
$strPageTitle = '在线支付' . '-' . $kekezu->_sys_config ['index_seo_title'];
